// 医院管理
import { deleteHospitalSet, deleteHospitalSetBatchRemove, getHospitalSet, getLock, postHospitalSave, putHospitalUpdate } from "@/api/hosp";
import { createSlice } from "@reduxjs/toolkit";
import React,{Key} from "react";
import { useSelector } from "react-redux";
const hospSlice = createSlice({
    name:"hosp",
    initialState:{
        setLoading:false,// 添加以及修改的状态
        getIsLoading:false,// 获取医院信息的状态

        hospSetList:[],// 医院设置列表
        "total": 1,// 总条数
        "pageSize": 5,// 每页显示的数量
        "current": 1,// 当前页
        "pages": 1,// 总页数,暂未用到
    },
    reducers:{
        changeSetLoading(state,{payload}){
            state.setLoading = payload;
        },
        // 更改获取医院的状态
        changeGetIsLoading(state,{payload}){
            state.getIsLoading = payload;
        },
        upHospSet(state,{payload}){
            state.hospSetList=payload.records;
            state.total=payload.total;
            state.pageSize=payload.size;
            state.current=payload.current;
            state.pages=payload.pages;
        }
    }
})
const {upHospSet,changeGetIsLoading,changeSetLoading} = hospSlice.actions;
// 返回医院配置的数据
export const useHospState = ()=> useSelector((state:any)=>({
        ...state.hosp,
        ...state.common
    }
));
// 获取医院信息
export const getHospitalSetSync = (page?:number,limit?:number,params?:any)=>{
    return async (dispatch:any,getState:any)=>{
        // 如果没有指定页数，将状态当中的current作为默认值
        page = (page || getState().hosp.current) as number;
        // 如果没有指定每页显示的数据，将状态当中的pageSize作为默认值
        limit = limit || getState().hosp.pageSize;
        if(page <1) return;
        // 准备获取数据
        dispatch(changeGetIsLoading(true));
        // 发送请求
        const result:any = await getHospitalSet(page,limit,params);  
        if(result.records.length===0 && page >1) {
            dispatch(getHospitalSetSync(page-1))
            return ;
        }    
        // 更新数据状态
        dispatch(upHospSet(result));
         // 获取数据完毕，并更新了数据状态
         dispatch(changeGetIsLoading(false))
    }
}
// 添加或修改医院信息
export const postOrPutHospitalSaveSync = (body:any)=>{
    return async (dispatch:any)=>{
        try{
            // 更新加载状态为：正在加载中
            dispatch(changeSetLoading(true));
            // 如果id存在，修改医院
            if(body.id)  await putHospitalUpdate(body);
            else await postHospitalSave(body);
            // 更新医院列表
            await dispatch(getHospitalSetSync(1));
            // 更新加载状态为：加载完毕
            dispatch(changeSetLoading(false));
        }catch(err){
            // 如果出现异常，将loading进行关闭
            dispatch(changeSetLoading(false));
            return Promise.reject(err)
        }
    }
}

// 根据ID删除医院设置信息
export const deleteHospitalSetAsync = (id:number)=>{
    return async (dispatch:any)=>{
        await deleteHospitalSet(id);
        dispatch(getHospitalSetSync());
    }
}

export const deleteHospitalSetBatchRemoveAsync = (key:Key[])=>{
    return async (dispatch:any)=>{
        await deleteHospitalSetBatchRemove(key);
        dispatch(getHospitalSetSync());
    }
}

// 根据ID对数据进行锁定与解锁
export const getLockAsync = (id:number,status:number)=>{
    return async (dispatch:any)=>{
        await getLock(id,status);
        dispatch(getHospitalSetSync());
    }
}
export default hospSlice.reducer;